package com.fr.annotation;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import com.fr.annotation.FruitColor.Color;

@Fruit(name = "apple", color = Color.yellow)
public class Apple {
    @FruitColor(name = "apple", fruitColor = Color.blue)
    private String name;
    @FruitColor(fruitColor = Color.blue)
    private Color fruitColor;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Color getFruitColor() {
        return fruitColor;
    }

    public void setFruitColor(Color fruitColor) {
        this.fruitColor = fruitColor;
    }

    @FruitShow(name = "show", describtion = "show describtion", param = @Fruit, returnType = "void")
    public void show() {

    }

    @FruitShow(name = "show Apple", describtion = "show Apple describtion", param = @Fruit, returnType = "void")
    public void show(Apple a) {

    }

    @FruitShow(name = "show String", describtion = "show String describtion", param = @Fruit, returnType = "void")
    public void show(String s) {

    }

    @Override
    public String toString() {
        return "Apple [name=" + name + ", color=" + fruitColor + "]";
    }

    public static void main(String[] args) throws NoSuchMethodException, SecurityException {
        Class<?> clazz = Apple.class;
        // //参数
        // Field[] fields = clazz.getDeclaredFields();
        // for(Field f : fields){
        // if(f.isAnnotationPresent(FruitName.class)){
        // FruitName fruitName = (FruitName) f.getAnnotation(FruitName.class);
        // System.out.println(fruitName.name());
        // }else if(f.isAnnotationPresent(FruitColor.class)){
        // FruitColor fruitColor= (FruitColor) f.getAnnotation(FruitColor.class);
        // System.out.println(fruitColor.fruitColor());
        // System.out.println(fruitColor.name());
        // }else if(f.isAnnotationPresent(Fruit.class)){
        // Fruit fruit= (Fruit) f.getAnnotation(Fruit.class);
        // System.out.println(fruit.color());
        // System.out.println(fruit.name());
        // }
        // }
        // //类
        // Annotation[] annotations = clazz.getAnnotations();
        // for(Annotation a : annotations){
        // if (a.annotationType().isAssignableFrom(Fruit.class)){
        // Fruit fruit = (Fruit) a;
        // System.out.println(fruit.color());
        // System.out.println(fruit.name());
        // }
        //
        // }
        // //类
        // Annotation a = clazz.getAnnotation(Fruit.class);
        // if (a != null){
        // Fruit fruit = (Fruit) a;
        // System.out.println(fruit.color());
        // System.out.println(fruit.name());
        // }
        // 方法
        Method method = clazz.getMethod("show", String.class);
        if (method != null) {
            if (method.isAnnotationPresent(FruitShow.class)) {
                FruitShow fruitShow = method.getAnnotation(FruitShow.class);
                System.out.println(fruitShow.name());
                System.out.println(fruitShow.describtion());
                System.out.println(fruitShow.param());
                System.out.println(fruitShow.returnType());
            }
        }
        // 方法
        Method[] methods = clazz.getMethods();
        for (Method m : methods) {
            if (m.isAnnotationPresent(FruitShow.class)) {
                FruitShow fruitShow = m.getAnnotation(FruitShow.class);
                System.out.println(fruitShow.name());
                System.out.println(fruitShow.describtion());
                System.out.println(fruitShow.param());
                System.out.println(fruitShow.returnType());
                Class<?>[] classes = m.getParameterTypes();
                for (Class<?> c : classes) {
                    System.out.println(c.getName());
                    Field[] fields = c.getDeclaredFields();
                    for (Field f : fields) {
                        if (f.isAnnotationPresent(FruitColor.class)) {
                            FruitColor fruitColor = (FruitColor) f.getAnnotation(FruitColor.class);
                            System.out.println(fruitColor.name());
                        }
                    }
                }

            }
        }
    }
}
